package cn.jbolt.codegen;

import cn.jbolt.codegen.model.CodeGen;
import cn.jbolt.codegen.model.CodeGenModelAttr;
import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.db.datasource.JBoltDataSourceUtil;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.permission.OnlySaasPlatform;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.core.service.base.JBoltBaseService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.tx.Tx;

@UnCheckIfSystemAdmin
@OnlySaasPlatform
@Path("/api/codegen")
public class CodeGenApiController extends JBoltApiBaseController {
    @Inject
    CodeGenService service;

    @Override
    protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
        return service;
    }

    /**
     * 数据源列表
     */
    public void datasources(){
        renderJBoltApiSuccessWithData(JBoltDataSourceUtil.me.getAllDatasource());
    }

    /**
     * 数据库列表
     */
    public void tables(){
        renderJBoltApiSuccessWithData(JBoltDataSourceUtil.me.getTableNameRemarkFullKvs(get("datasource"),getKeywords(),getInt("limit",10)));
    }

    /**
     * 重新刷新数据库列表
     */
    public void reloadTables() {
        renderJBoltApiRet(JBoltDataSourceUtil.me.reloadDatasourceTables(get("datasource")));
    }

    @Before(Tx.class)
    public void save(@Para("")CodeGen codeGen){
        renderJBoltApiRet(service.saveFront(codeGen));
    }

    @Before(Tx.class)
    public void update(@Para("")CodeGen codeGen){
        renderJBoltApiRet(service.updateFront(codeGen));
    }

    @Before(Tx.class)
    public void delete(){
        renderJBoltApiRet(deleteModelById());
    }

    /**
     * 数据
     * @param page
     * @param pageSize
     * @param keywords
     * @param isDeleted
     */
    public void datas(@Para(value = "page",defaultValue = "1")Integer page,
                      @Para(value = "pageSize",defaultValue = "15")Integer pageSize,
                      @Para("keywords")String keywords,
                      @Para("isDeleted")Boolean isDeleted
    ){
        renderJBoltApiSuccessWithData(service.paginateAdminDatas(null,true, page, pageSize,"update_time","desc",keywords,null,null,isDeleted,"main_table_name,author,remark,version_sn,remark"));
    }

    @Inject
    CodeGenModelAttrService codeGenModelAttrService;

    /**
     * 一个表中的属性列表
     */
    public void modelAttrs(){
        renderJBoltApiSuccessWithData(codeGenModelAttrService.getCodeGenModelAttrs(getLong(0),"sort_rank",false));
    }

    /**
     * 一个codeGen的详情
     */
    public void index(){
        renderJBoltApiRet(findModelById());
    }

    /**
     * 查看一个属性详情
     */
    public void modelAttrDetail(){
        CodeGenModelAttr modelAttr = codeGenModelAttrService.findById(getLong(0));
        if(modelAttr ==null){
            renderJBoltApiFail("数据不存在");
            return;
        }
        renderJBoltApiSuccessWithData(modelAttr);
    }

    /**
     * modelAttr的一个属性更新
     * @param codeGenModelAttr
     */
    @Before(Tx.class)
    public void updateModelAttrColumn(@Para("")CodeGenModelAttr codeGenModelAttr){
        renderJBoltApiRet(codeGenModelAttrService.updateOneColumn(codeGenModelAttr));
    }

    /**
     * 更新一个列
     * @param codeGenModelAttr
     */
    @Before(Tx.class)
    public void updateModelAttr(@Para("")CodeGenModelAttr codeGenModelAttr){
        renderJBoltApiRet(codeGenModelAttrService.updateFront(codeGenModelAttr));
    }

    /**
     * 重新同步字段
     */
    @Before(Tx.class)
    public void syncModelAttrs() {
        renderJson(codeGenModelAttrService.syncAttrs(getLong(0)));
    }

}
